/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.repository.entity.BankEntity;
import info.peigen.hotpot.business.account.manage.convertor.BankManageConvertor;
import info.peigen.hotpot.business.account.manage.repository.mapper.BankManageMapper;
import info.peigen.hotpot.common.core.domain.bank.Bank;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(BankManageRepository)</b>
 * 管理后台 "银行" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-08-29
 */
@SuppressWarnings("unused")
@Component
public class BankManageRepository extends AbstractBeetlSqlRepository<BankManageMapper, BankEntity> {

    public Bank active(String bankId) {
        Assert.isTrue(StrUtil.isNotEmpty(bankId), () -> new BusinessException(ResultCode.FAILURE, "激活时[bankId]不得为空!"));

        BankEntity entity = getMapper().findByBankId(bankId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!bankId=" + bankId));

        return BankManageConvertor.convertToDomain(entity);
    }

    public Bank lock(String bankId) {
        Assert.isTrue(StrUtil.isNotEmpty(bankId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[bankId]不得为空!"));

        BankEntity entity = getMapper().lockByBankId(bankId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!bankId为空!"));

        return BankManageConvertor.convertToDomain(entity);
    }

    public void removeByBankId(String bankId) {
        Assert.isTrue(StrUtil.isNotEmpty(bankId), () -> new BusinessException(ResultCode.FAILURE, "删除时[bankId]不得为空!"));

        getMapper().deleteByBankId(bankId);
    }

    public void store(Bank domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        BankEntity entity = BankManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}